//
// Created by guanyicheng on 2022/3/8.
//

#include "../../common.h"

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {

        // direction 标记方向
        // 0 从左到右  1 从上到下  2 从右到左  3 从下到上
        int value = 1, max = n * n, direction = 0;
        auto matrix = vector<vector<int>>(n, vector<int>(n));

        int x = 0, y = 0;
        while (value <= max) {
            matrix[x][y] = value ++;
            if (direction == 0) {
                // 从左到右填充
                y ++;
                // y的边界由x确定，达到这个边界后，从下个循环方向变为从上到下
                if (y == n - x - 1) {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 1) {
                // 从上到下
                x ++;
                // 当x,y 坐标是对角线时，x达到边界, 下个循环方向变为从右到左
                if (x == y) {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 2) {
                // 从右到左
                y --;
                // y的边界
                if (y == n - 1 - x) {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 3) {
                // 从下到上
                x --;
                // x 的边界
                if (x == y + 1) {
                    direction = (direction + 1) % 4;
                }
            }
        }
        return matrix;
    }
};

int main() {
    cout << Solution().generateMatrix(5).size() << endl;
}